package com.google.code.timetrail.gui;

import java.awt.Color;



import com.google.code.timetrail.backend.*;


public class StoreFrameBackend {
    private static final Color ERROR_COLOR=Color.pink;
    private static final Color GOOD_COLOR=Color.white;
    private Control gameControl;
    private Store store;
    private Inventory playerInv;

    private String engineTextField;
    private String fCapacitorTextField;
    private String foodTextField;
    private String hullPartsTextField;
    private String fuelCellsTextField;
    private String clothingTextField;
    private String ammunitionTextField;
    private int fuelCellsAmount;
    private int hullPartsAmount;
    private int fCapacitorAmount;
    private int ammunitionAmount;
    private int clothingAmount;
    private int foodAmount;
    private int engineAmount;
    private boolean engineValid;
    private boolean fCapacitorValid;
    private boolean fuelCellsValid;
    private boolean foodValid;
    private boolean hullPartsValid;
    private boolean clothingValid;
    private boolean ammunitionValid;
    
    public StoreFrameBackend(Control gameControl){
        this.store = new Store(gameControl);
        this.gameControl = gameControl;
        this.playerInv = this.gameControl.getInv();
        engineValid = true;
        fCapacitorValid = true;
        fuelCellsValid = true;
        foodValid = true;
        hullPartsValid = true;
        clothingValid = true;
        ammunitionValid = true;
        engineAmount = 0;
        fuelCellsAmount = 0;
        hullPartsAmount = 0;
        fCapacitorAmount = 0;
        ammunitionAmount = 0;
        clothingAmount = 0;
        foodAmount = 0;
    }

    public String getStoreLabelText() {
        
        return "STORE";
    }

    public String getQuantityLabelText() {
        
        return "Quantity";
    }

    public String getItemNameLabelText() {
        
        return "Item";
    }

    public String getPriceWeightLabelText() {
        
        return "Price / Weight";
    }

    public String getEngineLabelText() {
        
        return playerInv.getEngines().toString();
    }

    public String getFoodLabelText() {
        
        return playerInv.getFood().toString();
    }

    public String getClothingLabelText() {
        
        return playerInv.getTimeSuits().toString();
    }

    public String getAmmunitionLabelText() {
        
        return playerInv.getTimeBulletBills().toString();
    }

    public String getFCapacitorLabelText() {
        
        return playerInv.getfCapacitors().toString();
    }

    public String getHullPartsLabelText() {
        
        return playerInv.getHullParts().toString();
    }

    public String getFuelCellsLabelText() {
        
        return playerInv.getFuelCells().toString();
    }

    public String getFoodPriceWeight() {
        
        return ("$" + playerInv.getFood().getValue()) + " / " + (playerInv.getFood().getWeight() + " lb.");
    }

    public String getEnginePriceWeight() {

        return ("$" + playerInv.getEngines().getValue()) + (" N/A");
    }

    public String getClothingPriceWeight() {
        
        return ("$" + playerInv.getTimeSuits().getValue()) + " / " + (playerInv.getTimeSuits().getWeight() + " lb.");
    }

    public String getAmmunitionPriceWeight() {
        
        return ("$" + playerInv.getTimeBulletBills().getValue()) + " / " + (playerInv.getTimeBulletBills().getWeight() + " lb.");
    }

    public String getFCapacitorsPriceWeight() {
        
        return ("$" + playerInv.getfCapacitors().getValue()) + " / " + (playerInv.getfCapacitors().getWeight() + " lb.");
    }

    public String getHullPartsPriceWeight() {
        
        return ("$" + playerInv.getHullParts().getValue()) + " / " + (playerInv.getHullParts().getWeight() + " lb.");
    }

    public String getFuelCellsPriceWeight() {
        
        return ("$" + playerInv.getFuelCells().getValue()) + " / " + (playerInv.getFuelCells().getWeight() + " lb.");
    }

    public String getTotalWeightLabel() {
        
        return "Total Weight: " + getWeightTotal();
    }

    private int getWeightTotal() {

        int engineWeight = engineAmount * playerInv.getEngines().getWeight();
        int hullPartWeight = hullPartsAmount * playerInv.getHullParts().getWeight();
        int timeSuitWeight = clothingAmount * playerInv.getTimeSuits().getWeight();
        int timeBulletBillWeight = ammunitionAmount * playerInv.getTimeBulletBills().getWeight();
        int fuelCellWeight = fuelCellsAmount * playerInv.getFuelCells().getWeight();
        int fCapacitorWeight = fCapacitorAmount * playerInv.getfCapacitors().getWeight();
        int foodWeight = foodAmount * playerInv.getFood().getWeight();
        return engineWeight + hullPartWeight + timeSuitWeight + timeBulletBillWeight + fuelCellWeight + fCapacitorWeight + foodWeight;
    }

    public String getTotalCostLabel() {
        
        return "Total Cost: " + getCostTotal();
    }

    private int getCostTotal() {
        int engineCost = engineAmount * playerInv.getEngines().getValue();
        int hullPartCost = hullPartsAmount * playerInv.getHullParts().getValue();
        int timeSuitCost = clothingAmount * playerInv.getTimeSuits().getValue();
        int timeBulletBillCost = ammunitionAmount * playerInv.getTimeBulletBills().getValue();
        int fuelCellCost = fuelCellsAmount * playerInv.getFuelCells().getValue();
        int fCapacitorCost = fCapacitorAmount * playerInv.getfCapacitors().getValue();
        int foodCost = foodAmount * playerInv.getFood().getValue();
        return engineCost + hullPartCost + timeSuitCost + timeBulletBillCost + fuelCellCost + fCapacitorCost + foodCost;
    }

    public String getMoneyLabel() {
        
        return "Money:" + playerInv.getMoney().getCurrentAmount();
    }

    public String getNextButtonText() {
        
        return "Next >>";
    }



    public void pressNextButton() {

        store.setTempEngines(Integer.parseInt(engineTextField));
        store.setTempFCapacitors(Integer.parseInt(fCapacitorTextField));
        store.setTempFood(Integer.parseInt(foodTextField));
        store.setTempHullParts(Integer.parseInt(hullPartsTextField));
        store.setTempFuelCells(Integer.parseInt(fuelCellsTextField));
        store.setTempTimeSuits(Integer.parseInt(clothingTextField));
        store.setTempTimeBulletBills(Integer.parseInt(ammunitionTextField));
        
        store.canBuyItem(Integer.parseInt(fuelCellsTextField), gameControl.getInv().getFuelCells(), 
                store.canBuyItem(Integer.parseInt(fuelCellsTextField), gameControl.getInv().getFuelCells()));
        
        store.canBuyItem(Integer.parseInt(hullPartsTextField), gameControl.getInv().getHullParts(), 
                store.canBuyItem(Integer.parseInt(hullPartsTextField), gameControl.getInv().getHullParts()));
        
        store.canBuyItem(Integer.parseInt(foodTextField), gameControl.getInv().getFood(), 
                store.canBuyItem(Integer.parseInt(foodTextField), gameControl.getInv().getFood()));
        
        store.canBuyItem(Integer.parseInt(engineTextField), gameControl.getInv().getEngines(), 
                store.canBuyItem(Integer.parseInt(engineTextField), gameControl.getInv().getEngines()));
        
        store.canBuyItem(Integer.parseInt(fCapacitorTextField), gameControl.getInv().getfCapacitors(), 
                store.canBuyItem(Integer.parseInt(fCapacitorTextField), gameControl.getInv().getfCapacitors()));
        
        store.canBuyItem(Integer.parseInt(clothingTextField), gameControl.getInv().getTimeSuits(), 
                store.canBuyItem(Integer.parseInt(clothingTextField), gameControl.getInv().getTimeSuits()));
        
        store.canBuyItem(Integer.parseInt(ammunitionTextField), gameControl.getInv().getTimeBulletBills(), 
                store.canBuyItem(Integer.parseInt(ammunitionTextField), gameControl.getInv().getTimeBulletBills()));
         //TODO Set up moving to the next card
    }

    public String getBackButtonText() {

        return "Back <<";
    }

    public void pressBackButton() {
        // TODO set up moving to the previous card
        
    }

    public boolean canEnableNextButton() {
        
        return engineValid && fCapacitorValid && fuelCellsValid && foodValid && hullPartsValid && clothingValid && ammunitionValid;
    }
    
    public void setEngineTextField(String text) {

        this.engineTextField = text;
        
    }
    
    public Color getEngineTextFieldColor() {
        if(!isValidEntry(engineTextField)){
            engineValid = false;
            return ERROR_COLOR;
        }
        engineAmount = Integer.parseInt(engineTextField);
        if(engineAmount > playerInv.getEngines().getMaxAmount()){
            engineValid = false;
            return ERROR_COLOR;
        }
        if(canBuyFromStore()){
            engineValid = true;
            return GOOD_COLOR;
        } else {
            if(engineAmount != 0){
                engineValid = false;
                return ERROR_COLOR;
            }
            engineValid = true;
            return GOOD_COLOR;
        }
    }

    public void setfoodTextField(String text) {

        this.foodTextField = text;
        
    }

    public Color getFoodTextFieldColor() {
        if(!isValidEntry(foodTextField)){
            foodValid = false;
            return ERROR_COLOR;
        }
        foodAmount = Integer.parseInt(foodTextField);
        if(foodAmount > playerInv.getFood().getMaxAmount()){
            foodValid = false;
            return ERROR_COLOR;
        }
        if(canBuyFromStore()){
            foodValid = true;
            return GOOD_COLOR;
        } else {
            if(foodAmount != 0){
                foodValid = false;
                return ERROR_COLOR;
            }
                foodValid = true;
                return GOOD_COLOR;
        }
    }

    public void setClothingTextField(String text) {

        this.clothingTextField = text;
    }

    public Color getClothingTextFieldColor() {
        if(!isValidEntry(clothingTextField)){
            clothingValid = false;
            return ERROR_COLOR;
        }
        clothingAmount = Integer.parseInt(clothingTextField);
        if(clothingAmount > playerInv.getTimeSuits().getMaxAmount()){
            clothingValid = false;
            return ERROR_COLOR;
        }
        if(canBuyFromStore()){
            clothingValid = true;
            return GOOD_COLOR;
        } else {
            if(clothingAmount != 0){
                clothingValid = false;
                return ERROR_COLOR;
            }
            clothingValid = true;
            return GOOD_COLOR;
        }
    }

    public void setAmmunitionTextField(String text) {

        this.ammunitionTextField = text;
    }

    public Color getAmmunitionTextFieldColor() {
        if(!isValidEntry(ammunitionTextField)){
            ammunitionValid = false;
            return ERROR_COLOR;
        }
        ammunitionAmount = Integer.parseInt(ammunitionTextField);
        if(ammunitionAmount > playerInv.getTimeBulletBills().getMaxAmount()){
            ammunitionValid = false;
            return ERROR_COLOR;
        }
        if(canBuyFromStore()){
            ammunitionValid = true;
            return GOOD_COLOR;
        } else {
            if(ammunitionAmount != 0){
                ammunitionValid = false;
                return ERROR_COLOR;
            }
            ammunitionValid = true;
            return GOOD_COLOR;
        }
    }

    public void setFCapacitorTextField(String text) {

        this.fCapacitorTextField = text;
    }

    public Color getFCapacitorTextFieldColor() {
        if(!isValidEntry(fCapacitorTextField)){
            fCapacitorValid = false;
            return ERROR_COLOR;
        }
        fCapacitorAmount = Integer.parseInt(fCapacitorTextField);
        if(fCapacitorAmount > playerInv.getfCapacitors().getMaxAmount()){
            fCapacitorValid = false;
            return ERROR_COLOR;
        }
        if(canBuyFromStore()){
            fCapacitorValid = true;
            return GOOD_COLOR;
        } else {
            if(fCapacitorAmount != 0){
                fCapacitorValid = false;
                return ERROR_COLOR;
            }
            fCapacitorValid = true;
            return GOOD_COLOR;
        }
    }

    public void setHullPartsTextField(String text) {

        this.hullPartsTextField = text;
    }

    public Color getHullPartsTextFieldColor() {
        if(!isValidEntry(hullPartsTextField)){
            hullPartsValid = false;
            return ERROR_COLOR;
        }
        hullPartsAmount = Integer.parseInt(hullPartsTextField);
        if(hullPartsAmount > playerInv.getHullParts().getMaxAmount()){
            hullPartsValid = false;
            return ERROR_COLOR;
        }
        if(canBuyFromStore()){
            hullPartsValid = true;
            return GOOD_COLOR;
        } else {
            if(hullPartsAmount != 0){
                hullPartsValid = false;
                return ERROR_COLOR;
            }
            hullPartsValid = true;
            return GOOD_COLOR;
        }
    }

    public void setFuelCellsTextField(String text) {

        this.fuelCellsTextField = text;
    }

    public Color getFuelCellsTextFieldColor() {
        if(!isValidEntry(fuelCellsTextField)){
            fuelCellsValid = false;
            return ERROR_COLOR;
        }
        fuelCellsAmount = Integer.parseInt(fuelCellsTextField);
        if(fuelCellsAmount > playerInv.getFuelCells().getMaxAmount()){
            fuelCellsValid = false;
            return ERROR_COLOR;
        }
        if(canBuyFromStore()){
            fuelCellsValid = true;
            return GOOD_COLOR;
        } else {
            if(fuelCellsAmount != 0){
                fuelCellsValid = false;
                return ERROR_COLOR;
            }
            fuelCellsValid = true;
            return GOOD_COLOR;
        }
        
    }
    
    private boolean canBuyFromStore() {

        int weightRemaining = playerInv.getCapacity() - playerInv.getInvWeight();
        int playerMoney = playerInv.getMoney().getCurrentAmount();
        return (playerMoney >= getCostTotal() && weightRemaining >= getWeightTotal());
    }

    
    public boolean isValidEntry(String entry){
        try
        {
            int val=Integer.parseInt(entry);
            return val >= 0;
        }
        catch(NumberFormatException e)
        {
            return false;
        }
    }

}
